/**
 * 正在进行的状态
 * 记录用户已选 cell
 * 已选规格
 * @author gcq
 */
import {Cell} from "./cell";
import {Joiner} from "../../utils/Joiner";

class SkuPending {
    // 用户选择
    pending = []
    size

    constructor(size) {
        this.size = size
    }

    getCurrentSpecValues() {
        const values = this.pending.map(cell => {
            return cell ? cell.spec.value : null
        })
        return values
    }

    getMissingSpecKeyIndex() {
        const keysIndex = []
        for (let i = 0; i < this.size; i++) {
            if (!this.pending[i]) {
                keysIndex.push(i)
            }
        }
        return keysIndex
    }

    init(sku) {
        // this.size = sku.specs.length
        for (let i = 0; i < sku.specs.length; i++) {
            const cell = new Cell(sku.specs[i])
            this.insertCell(cell, i)
        }
    }

    getSkuCode() {
        const joiner = new Joiner("#")
        this.pending.forEach(cell => {
            const cellCode = cell.getCellCode()
            joiner.join(cellCode)
        })
        return joiner.getStr()
    }

    /**
     * 规格是否为空
     * @returns {boolean}
     */
    isIntact() {
        // if(this.size !== this.pending.length) {
        //   return false
        // }
        for (let i = 0; i < this.size; i++) {
            if (this._isEmptyPart(i)) {
                return false
            }
        }
        return true
    }

    _isEmptyPart(index) {
        return !this.pending[index]
    }

    insertCell(cell, x) {
        this.pending[x] = cell
    }

    removeCell(x) {
        this.pending[x] = null
    }

    findSelectedCellByX(x) {
        return this.pending[x]
    }

    isSelected(cell, x) {
        const pendingCell = this.pending[x]
        if (!pendingCell) {
            return false
        }
        return cell.id === pendingCell.id
    }
}

export {
    SkuPending
}